//middleware/auth.js下定义中间件
module.exports = (option,app)=>{
    return async function auth(ctx,next){
        //实现中间件
        // console.log(new Date());
        //只对/getToken地址进行放行
        console.log(ctx.url)    ///getToken?apikey=100
        ///getToken?apikey=eyJhbGciOiJIUzI1NiJ9.MTIzNDU2.jXGweo4iGdPngxob0efyzCoI5HMX04916NDDp7mrRms
        // console.log(option.title)
        var url = ctx.url;
        if(url == "/getToken"){
            await next()
        }else{
            //将传递过来的apikey和设置的apikey验证
            var apikey = ctx.query.apikey;
            try{
                var secret = await app.jwt.verify(apikey, app.config.jwt.secret);
                console.log(secret)     //123456
                await next();
            }catch(err){
                ctx.status = 401;
                ctx.body = {
                    code:400,
                    msg:"无效的apikey"
                }
            }
            
            console.log(apikey)     //eyJhbGciOiJIUzI1NiJ9.MTIzNDU2.jXGweo4iGdPngxob0efyzCoI5HMX04916NDDp7mrRms
            
        }
        // await next()    //没有这一行，会输出Data，有这一行，http://127.0.0.1:7001/页面会显示hello egg
        
    }
}